package github.kawaiior.yuanchuxiuzhen.common.item.weapon.refine;

import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World;

import javax.annotation.Nullable;
import java.util.List;

public class HurricaneFan extends RefineWeaponBase{

    @Override
    public float getAttackDamage(){
        return 4F;
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        if (worldIn.isRemote) {
            return super.onItemRightClick(worldIn, playerIn, handIn);
        }
        Vector3d lookVec = playerIn.getLookVec();
        double force = 5.0;
        boolean flag = playerIn.isSneaking();
        AxisAlignedBB boundingBox = playerIn.getBoundingBox().grow(10.0);
        List<Entity> entities = worldIn.getEntitiesWithinAABB(Entity.class, boundingBox);
        for (Entity entity : entities) {
            if (entity instanceof LivingEntity && !entity.equals(playerIn)) {
                LivingEntity livingEntity = (LivingEntity) entity;
                // 获取生物与玩家之间的方向向量
                double dx = livingEntity.getPosX() - playerIn.getPosX();
                double dy = livingEntity.getPosY() - playerIn.getPosY();
                double dz = livingEntity.getPosZ() - playerIn.getPosZ();

                // 计算单位向量
                double distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
                dx /= distance;
                dy /= distance;
                dz /= distance;

                if (flag){
                    livingEntity.setMotion(dx * force, dy * force, dz * force);
                }else {
                    double dotProduct = dx * lookVec.x + dy * lookVec.y + dz * lookVec.z;
                    if (dotProduct > 0.5) {
                        livingEntity.setMotion(dx * force, dy * force, dz * force);
                    }
                }
            }
        }
        return super.onItemRightClick(worldIn, playerIn, handIn);
    }

    @Override
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(new StringTextComponent("右键吹走前方生物，潜行+右键吹走周围的生物(至多吹走十个)"));
    }

}
